 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.OrderListResponse;
 import com.oanda.blackberry.restapi.Symbol;
 import com.oanda.blackberry.ui.BitmapButtonField;
 import com.oanda.blackberry.ui.ColoredEditField;
 import com.oanda.blackberry.ui.ColoredLabelField;
 import com.oanda.blackberry.ui.ColoredObjectChoiceField;
 import com.oanda.blackberry.ui.OandaMainScreen;
 import com.oanda.blackberry.ui.SpacerField;
 import com.oanda.blackberry.ui.SpacerManager;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import net.rim.device.api.system.Bitmap;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.FieldChangeListener;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Manager;
 import net.rim.device.api.ui.MenuItem;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.component.BasicEditField;
 import net.rim.device.api.ui.component.BitmapField;
 import net.rim.device.api.ui.component.ChoiceField;
 import net.rim.device.api.ui.component.Dialog;
 import net.rim.device.api.ui.component.LabelField;
 import net.rim.device.api.ui.component.Menu;
 import net.rim.device.api.ui.container.HorizontalFieldManager;
 import net.rim.device.api.ui.container.MainScreen;
 import net.rim.device.api.ui.decor.BackgroundFactory;
 
 public class OrderDetailsView
   implements AbstractViewPanel
 {
   DefaultController controller;
   BitmapButtonField submit;
   ColoredLabelField id;
   ColoredLabelField dir;
   ColoredLabelField symbol;
   ColoredEditField units;
   ColoredEditField price;
   ColoredEditField sl;
   ColoredEditField tp;
   ColoredEditField ts;
   ColoredEditField lb;
   ColoredEditField ub;
   Symbol lastSymbol;
   String[] expirityChoises = { "1 Hour", "2 Hours", "4 Hours", "8 Hours", "12 Hours", "16 Hours", "1 Day", "2 Days", "1 Week", "1 Month" };
 
   long[] expiryValues = { 3600L, 7200L, 14400L, 28800L, 43200L, 57600L, 86400L, 172800L, 604800L, 2592000L };
 
   int exptDefaultChoice = 8;
   ColoredObjectChoiceField expirity;
   OandaMainScreen screen;
   MenuItem changeOrder = new MenuItem("Change Order", 10, 1000)
   {
     public void run() {
       OrderDetailsView.this.changeOrder();
     }
   };
 
   MenuItem modifyOrder = new MenuItem("Modify", 10, 1001)
   {
     public void run() {
       OrderDetailsView.this.modify();
     }
   };
 
   MenuItem back = new MenuItem("Back", 10, 1001)
   {
     public void run() {
       OrderDetailsView.this.onClose();
     }
   };
 
   boolean isModify = false;
 
   boolean isSubmitAdded = false;
 
   public OrderDetailsView(DefaultController controller)
   {
     this.controller = controller;
   }
 
   private void init()
   {
     this.screen = new OandaMainScreen()
     {
       protected void makeMenu(Menu menu, int instance)
       {
         if (OrderDetailsView.this.isModify)
           menu.add(OrderDetailsView.this.changeOrder);
         else {
           menu.add(OrderDetailsView.this.modifyOrder);
         }
         menu.add(OrderDetailsView.this.back);
       }
     };
     this.screen.getMainManager().setBackground(BackgroundFactory.createSolidBackground(4473924));
     createBackground();
     initFields();
     setFonts();
     this.screen.updateTitle("Order Details");
 
     this.screen.add(new SpacerField(480, 25));
 
     HorizontalFieldManager orderIdM = new HorizontalFieldManager(1152921504606846976L);
 
     this.id.setMargin(2, 0, 0, 2);
     this.dir.setMargin(2, 0, 0, 60);
     this.symbol.setMargin(2, 0, 0, 60);
 
     ColoredLabelField orId = new ColoredLabelField(16119285, "Order #: ");
     orId.setFont(DefaultController.getComponentFont());
     orId.setMargin(2, 0, 0, 15);
     orderIdM.add(orId);
     orderIdM.add(this.id);
     orderIdM.add(this.dir);
     orderIdM.add(this.symbol);
     this.screen.add(orderIdM);
 
     this.screen.add(new SpacerField(480, 15));
 
     HorizontalFieldManager details = new HorizontalFieldManager(1152921504606846976L);
     SpacerManager d1 = new SpacerManager(240, 74);
     SpacerManager d2 = new SpacerManager(240, 74);
     this.screen.add(details);
 
     details.add(d1);
     details.add(d2);
     HorizontalFieldManager uhm = new HorizontalFieldManager();
     BitmapField uBitmap = new BitmapField(Bitmap.getBitmapResource("units-label-72x35.png"));
     uhm.add(uBitmap);
     uhm.add(this.units);
     d1.add(uhm);
     uBitmap.setPadding(3, 0, 3, 0);
     this.units.setPadding(10, 8, 8, 13);
 
     HorizontalFieldManager ehm = new HorizontalFieldManager();
     BitmapField eBitmap = new BitmapField(Bitmap.getBitmapResource("expiry-label-72x35.png"));
     ehm.add(eBitmap);
     ehm.add(this.expirity);
     d1.add(ehm);
     eBitmap.setPadding(3, 0, 3, 0);
     this.expirity.setPadding(10, 25, 8, 4);
 
     HorizontalFieldManager phm = new HorizontalFieldManager();
     BitmapField pBitmap = new BitmapField(Bitmap.getBitmapResource("quote-label-72x35.png"));
     phm.add(pBitmap);
     phm.add(this.price);
     d2.add(phm);
     pBitmap.setPadding(4, 0, 4, 0);
     this.price.setPadding(10, 8, 8, 10);
 
     this.screen.add(new BitmapField(Bitmap.getBitmapResource("separator-line-transperant-480-18.png")));
 
     HorizontalFieldManager limits = new HorizontalFieldManager(1152921504606846976L);
     SpacerManager v1 = new SpacerManager(240, 122);
     SpacerManager v2 = new SpacerManager(240, 122);
     limits.add(v1);
     limits.add(v2);
     this.screen.add(limits);
 
     this.tp.setPadding(10, 8, 8, 15);
     this.sl.setPadding(10, 8, 8, 15);
     this.ts.setPadding(10, 8, 8, 15);
 
     this.ub.setPadding(10, 8, 8, 10);
     this.lb.setPadding(10, 8, 8, 10);
 
     HorizontalFieldManager tpm = new HorizontalFieldManager();
     BitmapField tpBitmap = new BitmapField(Bitmap.getBitmapResource("tp-label-72x35.png"));
     tpm.add(tpBitmap);
     tpm.add(this.tp);
     tpBitmap.setPadding(3, 0, 3, 0);
     v1.add(tpm);
 
     HorizontalFieldManager slm = new HorizontalFieldManager();
     BitmapField slBitmap = new BitmapField(Bitmap.getBitmapResource("sl-label-72x35.png"));
     slm.add(slBitmap);
     slm.add(this.sl);
     slBitmap.setPadding(3, 0, 3, 0);
     v1.add(slm);
 
     HorizontalFieldManager tsm = new HorizontalFieldManager();
     BitmapField tsBitmap = new BitmapField(Bitmap.getBitmapResource("ts-label-72x35.png"));
     tsm.add(tsBitmap);
     tsm.add(this.ts);
     tsBitmap.setPadding(3, 0, 3, 0);
     v1.add(tsm);
 
     HorizontalFieldManager ubm = new HorizontalFieldManager();
     BitmapField ubBitmap = new BitmapField(Bitmap.getBitmapResource("ub-label-72x35.png"));
     ubm.add(ubBitmap);
     ubm.add(this.ub);
     ubBitmap.setPadding(3, 0, 3, 0);
     v2.add(ubm);
 
     HorizontalFieldManager lbm = new HorizontalFieldManager();
     BitmapField lbBitmap = new BitmapField(Bitmap.getBitmapResource("lb-label-72x35.png"));
     lbm.add(lbBitmap);
     lbm.add(this.lb);
     lbBitmap.setPadding(3, 0, 3, 0);
     v2.add(lbm);
 
     this.submit.setChangeListener(new FieldChangeListener()
     {
       public void fieldChanged(Field field, int context)
       {
         OrderDetailsView.this.changeOrder();
       }
     });
   }
 
   private void setOrderDetails(final OrderListResponse order)
   {
     if (order == null) {
       DefaultController.inform("no order details");
     }
     try
     {
       Runnable r = new Runnable() {
         private final OrderListResponse _order=order;
 
         public void run() { if (this._order != null) {
             if (OrderDetailsView.this.screen != null) {
               OrderDetailsView.this.screen.updateTitle("Order Details - " + Long.toString(this._order.getId()));
             }
 
             OrderDetailsView.this.id.setText(Long.toString(this._order.getId()));
             OrderDetailsView.this.units.setText(Integer.toString(this._order.getUnits()));
 
             if (this._order.getType().startsWith("B"))
               OrderDetailsView.this.dir.setText("LONG");
             else {
               OrderDetailsView.this.dir.setText("SHORT");
             }
             OrderDetailsView.this.symbol.setText(this._order.getSymbol());
             OrderDetailsView.this.price.setText(Double.toString(this._order.getPrice()));
 
             String sL = Double.toString(this._order.getStopLoss());
             if ((sL == null) || ((sL != null) && (sL.length() == 0))) sL = "---";
 
             String tP = Double.toString(this._order.getTakeProfit());
             if ((tP == null) || ((tP != null) && (tP.length() == 0))) tP = "---";
 
             String tS = Double.toString(this._order.getTrailingStop());
             if ((tS == null) || ((tS != null) && (tS.length() == 0))) {
               tS = "---";
             }
 
             String lB = Double.toString(this._order.getLowLimit());
             if ((lB == null) || ((lB != null) && (lB.length() == 0))) lB = "---";
 
             String uB = Double.toString(this._order.getHighLimit());
             if ((uB == null) || ((uB != null) && (uB.length() == 0))) uB = "---";
 
             OrderDetailsView.this.sl.setText(sL);
             OrderDetailsView.this.tp.setText(tP);
             OrderDetailsView.this.ts.setText(tS);
 
             OrderDetailsView.this.lb.setText(lB);
             OrderDetailsView.this.ub.setText(uB);
 
             Symbol symbol = Model.getModel().getSymbol(this._order.getSymbol());
             if ((OrderDetailsView.this.ts != null) && (OrderDetailsView.this.ts.getText() != null) && (!OrderDetailsView.this.ts.getText().equals("---")) && (OrderDetailsView.this.ts.getText().length() > 0) && (symbol != null)) {
               OrderDetailsView.this.lastSymbol = symbol;
               String tsT = OrderDetailsView.this.ts.getText();
               double tsD = Double.parseDouble(tsT);
               double d = symbol.getPips(tsD);
               OrderDetailsView.this.ts.setText(Double.toString(d));
             }
 
             long e = this._order.getExpirity();
             long time = this._order.getTime();
             int se = 0;
 
             for (int i = 0; i < OrderDetailsView.this.expiryValues.length; i++) {
               if (OrderDetailsView.this.expiryValues[i] <= e - time)
                 continue;
               se = i;
               break;
             }
 
             if (OrderDetailsView.this.expirity.getSize() > se)
               OrderDetailsView.this.expirity.setSelectedIndex(se);
           }
         }
       };
       this.controller.invokeLater(r);
     }
     catch (NullPointerException n) {
     }
   }
 
   protected void onUiEngineAttached(boolean attached) {
     if (!attached)
     {
       initFields();
     }
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
     if (evt.getPropertyName().equals("setOrderDetails")) {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof OrderListResponse)))
         setOrderDetails((OrderListResponse)evt.getNewValue());
     }
     else if (evt.getPropertyName().equals("showOrderDetailsView")) {
       init();
       if (this.screen != null)
         this.controller.pushScreen(this.screen);
       else
         DefaultController.inform("odv sc is null");
     }
     else if (evt.getPropertyName().equals("hideOrdersDetailsView")) {
       onClose();
     }
   }
 
   private void changeOrder() {
     if (4 == Dialog.ask(3, "Are you sure you want to change this order?"))
     {
       String orderId = getOrderId();
       String accountId = getAccountId();
 
       String optStopLoss = getOptStopLoss();
       String optTakeProfit = getOptTakeProfit();
       String optTrailingStop = getOptTrailingStop();
       if ((this.lastSymbol != null) && (optTrailingStop != null) && (optTrailingStop.length() > 0)) {
         double d = Double.parseDouble(optTrailingStop);
         double tsInPippettes = this.lastSymbol.getPippettes(d);
         int tsInt = (int)tsInPippettes;
         optTrailingStop = Integer.toString(tsInt);
       }
 
       String symb = this.symbol.getText();
       String units = getUnits();
       String price = getPrice();
       String expiry = getExpiry();
       String type = getType();
 
       String optLB = getLB();
       String optUB = getUB();
 
       this.controller.changeOrder(orderId, accountId, symb, type, units, price, expiry, optLB, optUB, optStopLoss, optTakeProfit, optTrailingStop);
     }
   }
 
   private String getType()
   {
     if (this.dir.getText().equals("LONG"))
     {
       return "buy";
     }
 
     return "sell";
   }
 
   private String getOrderId()
   {
     return this.id.getText();
   }
 
   private String getUnits() {
     return this.units.getText();
   }
 
   private String getPrice() {
     return this.price.getText();
   }
 
   private String getExpiry() {
     long now = System.currentTimeMillis() + this.expiryValues[this.expirity.getSelectedIndex()] * 1000L;
     long unixExpiry = now / 1000L;
 
     return Long.toString(unixExpiry);
   }
 
   private String getLB() {
     String lowerPrice = this.lb.getText();
     double lP = (0.0D / 0.0D);
     if ((lowerPrice != null) && (lowerPrice.length() > 0))
       try {
         lP = Double.parseDouble(lowerPrice);
         if (lP == 0.0D)
           return "";
       }
       catch (NumberFormatException ne) {
         DefaultController.inform("error in parsing lower bound to double");
       }
     else {
       lowerPrice = "";
     }
     return lowerPrice;
   }
 
   private String getUB() {
     String highPrice = this.ub.getText();
     double uP = (0.0D / 0.0D);
     if ((highPrice != null) && (highPrice.length() > 0))
       try {
         uP = Double.parseDouble(highPrice);
         if (uP == 0.0D)
           return "";
       }
       catch (NumberFormatException ne) {
         DefaultController.inform("error in parsing upper bound to double");
       }
     else {
       highPrice = "";
     }
     return highPrice;
   }
 
   private String getAccountId() {
     return Model.getModel().getActiveAccountId();
   }
 
   private String getOptStopLoss() {
     String stopLoss = this.sl.getText();
     double sl = (0.0D / 0.0D);
     if ((stopLoss != null) && (stopLoss.length() > 0))
       try {
         sl = Double.parseDouble(stopLoss);
         if (sl == 0.0D)
           return "";
       }
       catch (NumberFormatException ne) {
         DefaultController.inform("error in parsing stop loss to double");
       }
     else {
       stopLoss = "";
     }
     return stopLoss;
   }
 
   private String getOptTrailingStop() {
     String trailingStop = this.ts.getText();
     double ts = (0.0D / 0.0D);
     if ((trailingStop != null) && (trailingStop.length() > 0))
       try {
         ts = Double.parseDouble(trailingStop);
         if (ts == 0.0D)
           return "";
       }
       catch (NumberFormatException ne) {
         DefaultController.inform("error in parsing trailing stop to double");
       }
     else {
       trailingStop = "";
     }
     return trailingStop;
   }
 
   private String getOptTakeProfit() {
     String takeProfit = this.tp.getText();
     double takeP = (0.0D / 0.0D);
     if ((takeProfit != null) && (takeProfit.length() > 0))
       try {
         takeP = Double.parseDouble(takeProfit);
         if (takeP == 0.0D)
           return "";
       }
       catch (NumberFormatException ne) {
         DefaultController.inform("error in parsing trailing stop to double");
       }
     else {
       takeProfit = "";
     }
     return takeProfit;
   }
 
   public boolean onClose()
   {
     Runnable r = new Runnable()
     {
       public void run() {
         OrderDetailsView.this.freeResourses();
         if (OrderDetailsView.this.screen != null) {
           OrderDetailsView.this.controller.popScreen(OrderDetailsView.this.screen);
         }
         OrderDetailsView.this.screen = null;
       }
     };
     this.controller.invokeLater(r);
     return true;
   }
 
   private void createBackground() {
     Bitmap bg = Bitmap.getBitmapResource("order-modify-480x360.png");
     if (this.screen != null)
       this.screen.getMainManager().setBackground(BackgroundFactory.createBitmapBackground(bg));
   }
 
   private void setFonts() {
     Font font = DefaultController.getComponentFont();
     this.id.setFont(font);
 
     this.units.setFont(font);
     this.price.setFont(font);
     this.expirity.setFont(font);
 
     this.symbol.setFont(font);
 
     this.ub.setFont(font);
     this.lb.setFont(font);
 
     this.tp.setFont(font);
     this.ts.setFont(font);
 
     this.sl.setFont(font);
 
     this.dir.setFont(font);
   }
 
   private void modify()
   {
     this.isModify = true;
 
     setFieldsBg();
 
     setFieldsToEditable();
 
     addSubmit();
   }
 
   private void setFieldsToEditable()
   {
     this.tp.setEditable(true);
     this.ts.setEditable(true);
     this.ub.setEditable(true);
     this.lb.setEditable(true);
     this.sl.setEditable(true);
 
     this.price.setEditable(true);
     this.units.setEditable(true);
     this.expirity.setEditable(true);
 
     this.expirity.setWidth(130);
     this.expirity.setPadding(8, 40, 8, 4);
 
     if (this.screen != null)
       this.screen.invalidate();
   }
 
   private void setFieldsBg()
   {
     Bitmap bg2 = Bitmap.getBitmapResource("limit-bg-transparent-166x35.png");
 
     this.tp.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.ts.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.ub.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.lb.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.sl.setBackground(BackgroundFactory.createBitmapBackground(bg2));
 
     this.units.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.expirity.setBackground(BackgroundFactory.createBitmapBackground(bg2));
     this.price.setBackground(BackgroundFactory.createBitmapBackground(bg2));
 
     this.tp.setTitleColor(8956419);
     this.ts.setTitleColor(16762898);
     this.ub.setTitleColor(8936959);
     this.lb.setTitleColor(8936959);
     this.sl.setTitleColor(14287362);
     this.units.setTitleColor(16119285);
     this.price.setTitleColor(16119285);
     this.expirity.setTitleColor(16119285);
 
     this.tp.setFontColor(3355443);
     this.ts.setFontColor(3355443);
     this.ub.setFontColor(3355443);
     this.lb.setFontColor(3355443);
     this.sl.setFontColor(3355443);
     this.units.setFontColor(3355443);
     this.price.setFontColor(3355443);
     this.expirity.setFontColor(3355443);
   }
 
   private void initFields() {
     this.isModify = false;
 
     this.submit = new BitmapButtonField(Bitmap.getBitmapResource("submit-button-hover.png"), Bitmap.getBitmapResource("submit-button-p8.png"), "Submit", 12884967424L);
 
     this.id = new ColoredLabelField(16119285, "Order #: ", 45035996273704960L);
 
     this.dir = new ColoredLabelField(16119285, "", 45035996273704960L);
     this.symbol = new ColoredLabelField(16119285, "", 45035996273704960L);
 
     this.units = new ColoredEditField(16119285, "", "", 15, 9007199338627072L);
     this.price = new ColoredEditField(16119285, "", "", 25, 9007199506399232L);
 
     this.sl = new ColoredEditField(14287362, "", "", 15, 9007199506399232L);
     this.tp = new ColoredEditField(8956419, "", "", 15, 9007199506399232L);
     this.ts = new ColoredEditField(16762898, "", "", 15, 9007199506399232L);
 
     this.lb = new ColoredEditField(8936959, "", "", 15, 9007199506399232L);
     this.ub = new ColoredEditField(8936959, "", "", 15, 9007199506399232L);
 
     this.expirity = new ColoredObjectChoiceField(16119285, 16119285, "", this.expirityChoises, 130, 20);
 
     this.units.setLabel("");
     this.price.setLabel("");
 
     this.tp.setEditable(false);
     this.ts.setEditable(false);
     this.ub.setEditable(false);
     this.lb.setEditable(false);
     this.sl.setEditable(false);
 
     this.units.setEditable(false);
     this.price.setEditable(false);
     this.expirity.setEditable(false);
 
     this.tp.setBackground(null);
     this.ts.setBackground(null);
     this.ub.setBackground(null);
     this.lb.setBackground(null);
     this.sl.setBackground(null);
 
     this.units.setBackground(null);
     this.price.setBackground(null);
     this.expirity.setBackground(null);
 
     this.tp.setFontColor(8956419);
     this.ts.setFontColor(16762898);
     this.ub.setFontColor(8936959);
     this.lb.setFontColor(8936959);
     this.sl.setFontColor(14287362);
     this.units.setFontColor(16119285);
     this.price.setFontColor(16119285);
     this.expirity.setFontColor(16119285);
     this.expirity.setTitleColor(16119285);
     this.expirity.setWidth(130);
 
     if (this.isSubmitAdded) {
       try {
         if (this.screen != null)
           this.screen.delete(this.submit);
       } catch (IllegalArgumentException e) {
       }
       this.isSubmitAdded = false;
     }
 
     if (this.screen != null)
       this.screen.invalidate();
   }
 
   private void addSubmit()
   {
     this.isSubmitAdded = true;
     if (this.screen != null)
       this.screen.add(this.submit);
   }
 
   private void freeResourses() {
     this.isSubmitAdded = false;
 
     this.submit = null;
 
     this.id = null;
     this.dir = null;
     this.symbol = null;
 
     this.units = null;
     this.price = null;
 
     this.sl = null;
     this.tp = null;
     this.ts = null;
 
     this.lb = null;
     this.ub = null;
 
     this.expirity = null;
     this.lastSymbol = null;
   }
}

